<?php

namespace App\Model\Business;

use App\Model\System\Organization;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Schedule extends Model
{
    # 班课
    const LESSON = 2;
    # 约课
    const APPOINT = 1;

    # 以约
    const VALID = 1;
    # 未约
    const UNVALID = 0;

    # 数据表
    protected $table = 'schedules';

    # PK
    protected $primaryKey = 'scheduleid';

    # fillable fields
    protected $fillable = [
        'type',
        'name',
        'status',
        'classeid',
        'classtypeid',
        'folderid',
        'foldername',
        'teacheruid',
        'begintime',
        'endtime',
        'seatnum',
        'record',
        'live',
        'replay',
        'webcast',
        'quotas',
        'usequotas',
        'coursetypeid',
        'coursetypename',
        'labels',
        'remark',
        'levelid',
        'eeosync',
        'eeosyncvideo',
        'eeosynccomment'
    ];

    /**
     * 设定排课表的类型
     *
     * @param  string  $value
     * @return void
     */
    public function setTypeAttribute($value)
    {
        $this->attributes['type'] = empty($value) ? self::APPOINT : $value;
    }

    public function valide()
    {
        $this->status = self::VALID;
    }

    public function unvalide()
    {
        $this->status = self::UNVALID;
    }

    /**
     * 学生预约
     */
    public function appoint($classeid=null)
    {
        if($classeid){
            $this->classeid = $classeid;
        }
        $this->increment('usequotas');
        $this->valide();
        return $this->save();
    }

    /**
     * 取消预约
     */
    public function unppoint($classeid=null)
    {
        if($classeid){
            $this->classeid = null;
        }
        # 更新排课状态

        $student = DB::table('schedules_students')
            ->where('identity',1)
            ->where('scheduleid',$this->scheduleid)
            ->first();
        $this->decrement('usequotas');
        if(is_null($student)){
            $this->unvalide();
        }
        return $this->save();
    }


    /**
     * 计算自身的课时数
     * @return float
     */
    public function realSchedules()
    {
        return self::calculRealSchedules($this->begintime, $this->endtime);
    }

    /**
     * 获取真实的课时，根据分钟数计算后的真实值
     * @param null $begintime
     * @param null $endtime
     * @return float
     */
    public static function calculRealSchedules($begintime=null, $endtime=null)
    {
        # 获取配置分钟数
        static $orgmins = 0;
        if($orgmins == 0){
            $org = Organization::find(1);
            $orgmins = $org->courseminutes*60;
        }
        # 计算课节
        $end = strtotime($endtime);
        $begin = strtotime($begintime);
        $culmins = round(($end-$begin)/$orgmins, 1);
        return $culmins;
    }

    /**
     * 获取某个学生最近一次上课时间
     * @param Request $request
     * @param $id
     * @return
     */
    static function getStudentLastSchedule($id)
    {
        $last = '';
        $now = date('Y-m-d H:i:s');
        $scheduleids = ScheduleStuden::where('studentuid',$id)->pluck('scheduleid')->toArray();
        if(!empty($scheduleids)){
            $last = Schedule::whereIn('scheduleid', $scheduleids)
                ->where('begintime', '<', $now)
                ->orderBy('begintime', 'desc')
                ->limit(1)->value('begintime');
            if($last == []){
                $last = '';
            }
        }
        return $last;
    }
}
